
/*var r = new Request("http://livres-bd.shop.ebay.fr/Livres-BD-Revues-/267/i.html?LH_TitleDesc=0&LH_PrefLoc=0&_nkw=full+metal+panic&_catref=1&_fln=1",
					Request.GET)


var body = "";
var head = "";
var page = "";

var doc = "";

r.onload = function(o,req)
{
	var xmlreq = req.getXMLHttpRequest();
	// console.log(xmlreq.responseXML)
	Storage.set("page",xmlreq.responseText)

	page = Storage.get("page")
	
	
	// Remplacement des liens pour qu'aucun chargement soit effectué
	
	var liste_src = [
		["script", "src", "_src"],
		["link", "href", "_href"],
		["img", "src", "_src"]
	];
	
	for (var i = 0; i < liste_src.length; i++)
	{
		var tag = liste_src[i]
		var nametag = tag[0]
		var attr = tag[1]
		var new_attr = tag[2]
		
		var regexp = new RegExp("<"+nametag+"([^>]*)"+attr+'="([^"]*)"([^>]*)>','g')		
		
		page = page.replace(regexp,"<"+nametag+"$1"+new_attr+'="$2"$3>')
	}
	
	// Remplacement du style par _style
	regexp_style = new RegExp('<([a-zA-Z-]*)([^>]*)style="([^"]*)"([^>]*)>','g')
	page = page.replace(regexp_style,"<$1$2"+'_style="$3"$4>')
	
	// Remplacement des balises javascript sans src, pour éviter leur exécution
	regexp_script_no_src = /<script type="text\/javascript">/g
	page = page.replace(regexp_script_no_src,'<script type="text/javascript" src="blank.js">')
	
	// Parsage de l'HTML
	doc = createDocumentFromString(page)
	
	
	// Suppression des src blank.js factifs
	var scripts = doc.getElementsByTagName("script")
	for (var i = 0; i < scripts.length; i++)
	{
		if (scripts[i].getAttribute("src") == "blank.js")
			scripts[i].removeAttribute("src");
	}
	
	
	var items = Website.websites[0].getItems(doc);
	Website.websites[0].getPages(doc)
};

r.start();


function createDocumentFromString(content)
{
    var iframe = document.createElement("iframe");
        document.body.appendChild(iframe); 

	var doc = iframe.document;
    
    if(iframe.contentDocument)
        doc = iframe.contentDocument; // For NS6
    else if(iframe.contentWindow)
        doc = iframe.contentWindow.document; // For IE5.5 and IE6
    // Put the content in the iframe
    doc.open();
    doc.writeln(content);
    doc.close(); 

	return doc;
	
}
*/
(new I18n()).init();

var tw = new LeBonCoin();
tw.oninit = function()
{
	document.searchOnTest = function(keyword)
	{
		var s = tw.newSearch({search: keyword});
		var totalItems = 0;
		s.onpageload = function(p)
		{
			totalItems += p.getTotalItems();
			console.log("Nombre d'objets: "+totalItems+" - Nb pages: "+p.getTotalPages());
			
			var items = p.getItems();
			
			var id = items[0].getId();
			
			var url = tw.getPageUrlForId(id);
			
			var r = new Request(url.url, url.type, url.params);
			r.onload = function(e) {
				
				var doc = e.getRequest().getResponseDocument();
				console.log(tw.parseDomAndGetPageById(id, doc));
			}
			r.start();
			
		};
		
		s.start();
	}
};

tw.init();

chrome.browserAction.onClicked.addListener(function() { chrome.tabs.create({url: "pages/dashboard.html"}) });
// Base 64 encode
/*
var r = new Request("http://sstatic.net/stackoverflow/img/favicon.ico",Request.GET);
//r.responseType = "blob"
r.onload = function(e)
{
 	var str = e.getRequest().getResponseText();
 	
 	var total = 0
 	var newstr = "";
 	for (var i = 0; i < str.length; i++)
 	{
 		var b = (str.charCodeAt(i) & 0xFF)
 		total += b;
 		newstr += String.fromCharCode(b)
 	}
 	console.log("Total: "+total)
 	
 	var teststr = "AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCwAAAAAAAAAAAAAAAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAAAAAAGxsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/AAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAbGxt/2xsbf8AAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAbGxt/2x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kAAJAJAACfAQAA4AcAAOAHAADggwAA/gEAAPABAADwIQAA+MP///+H////DwAA/g8AAA=="
 	var b64str = Base64.encode(newstr);
 	if (teststr == b64str)
 		console.log("true !")
 	else
 		console.log("false !")
 	document.getElementById("img_test").src = "data:image/vnd.microsoft.icon;base64,"+b64str

};
r.start();
*/
